package com.lishe.lishe.lishe.module.me.fragments;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.lishe.lishe.R;
import com.lishe.lishe.asynchttp.LSWebHelper;
import com.lishe.lishe.lishe.common.Globals;
import com.lishe.lishe.lishe.module.me.bean.MyAssetsObject;
import com.lishe.lishe.lishe.module.me.http.HttpUserCenter;
import com.lishe.lishe.lishe.module.recharge.activity.RechargeIntegralActivity;
import com.lishe.lishe.lishe.utils.LoadingUtils;

import org.json.JSONObject;

import java.util.HashMap;

/**
 * Created by zhcasecom on 2017/8/17.
 * 福利积分
 */

public class FragmentPoints extends Fragment implements View.OnClickListener {
    private String TAG = getClass().getSimpleName();
    private TextView balance, common_points, limit_points;
    private Button btn_charge;

    public static FragmentPoints newInstance() {
        FragmentPoints instance = new FragmentPoints();
        Bundle bundle = new Bundle();
        instance.setArguments(bundle);
        return instance;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_my_points, container, false);
        balance = view.findViewById(R.id.jifen_txt);
        common_points = view.findViewById(R.id.common_points_txt);
        limit_points = view.findViewById(R.id.limit_points_txt);
        btn_charge = view.findViewById(R.id.btn_charge);
        btn_charge.setOnClickListener(this);
        return view;
    }

    private void initData() {
        try {
            LoadingUtils.createLoadingDialog(getActivity(), "加载中...",true);
            HashMap params = new HashMap();
            HttpUserCenter.getBalance(params, response);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    LSWebHelper.WebCallBack response = new LSWebHelper.WebCallBack() {
        @Override
        public void onSuccess(JSONObject dataJson, String dataString, String message) {
            LoadingUtils.closeDialog();
            MyAssetsObject myAssetsOb = JSON.parseObject(dataString, MyAssetsObject.class);
            setText(myAssetsOb);

        }

        @Override
        public void onFailure(String message) {
            Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show();
            LoadingUtils.closeDialog();
        }
    };

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_charge:
                //跳转到充值界面
                RechargeIntegralActivity.jumpForResult(getActivity(), Globals.REQUSE_CODE_RechargeIntegralActivity);
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == getActivity().RESULT_OK) {
            if (requestCode == Globals.REQUSE_CODE_RechargeIntegralActivity) {
                Log.e("开始刷新", "go");
            }
        }
    }

    public void setText(MyAssetsObject ob) {
        balance.setText(ob.getBalance());
        common_points.setText(ob.getCommonAmount());
        limit_points.setText(ob.getLimitAmount());
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
    }
}
